/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.spend.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.property.domain.Property1;
import me.zhengjie.modules.spend.domain.Spend;
import me.zhengjie.modules.spend.service.SpendService;
import me.zhengjie.modules.spend.service.dto.SpendQueryCriteria;
import me.zhengjie.modules.utils.FileMover;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2022-04-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "spend管理")
@RequestMapping("/api/spend")
public class SpendController {

    private final SpendService spendService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('spend:list')")
    public void download(HttpServletResponse response, SpendQueryCriteria criteria) throws IOException {
        spendService.download(spendService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询spend")
    @ApiOperation("查询spend")
    @PreAuthorize("@el.check('spend:list')")
    public ResponseEntity<Object> query(SpendQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(spendService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增spend")
    @ApiOperation("新增spend")
    @PreAuthorize("@el.check('spend:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Spend resources){
        return new ResponseEntity<>(spendService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改spend")
    @ApiOperation("修改spend")
    @PreAuthorize("@el.check('spend:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Spend resources){
        spendService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除spend")
    @ApiOperation("删除spend")
    @PreAuthorize("@el.check('spend:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        spendService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("批量导入信息")
    @ApiOperation("批量导入信息模板")
    @AnonymousAccess
    @PostMapping(value = "/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return spendService.upload(file);
    }

    @Log("导出信息模板")
    @ApiOperation("导出信息模板")
    @GetMapping(value = "/export")
    @AnonymousAccess
    public void exportTemplate(HttpServletResponse response, Property1 criteria) throws IOException {
        spendService.exportTemplate(response);
    }


    @ApiOperation("每月总数")
    @AnonymousAccess
    @GetMapping(value = "/everyMonth/{dateNum}")
    public List<Map<Object,Object>> everyMonth(@PathVariable Integer dateNum) {
        return spendService.everyMonth(dateNum);
    }


    @ApiOperation("获取所有数据")
    @AnonymousAccess
    @GetMapping(value = "/getData")
    public List<Spend> getData() {
        return spendService.getData();
    }


    @ApiOperation("获取消费类型数据")
    @AnonymousAccess
    @GetMapping(value = "/getSpendTypeNum/{type}")
    public List<Map<Object,Object>> getSpendTypeNum(@PathVariable String type) { return spendService.getSpendTypeNum(type);}

    @ApiOperation("获取支付类型数据")
    @AnonymousAccess
    @GetMapping(value = "/getPayTypeNum/{type}")
    public List<Map<Object,Object>> getPayTypeNum(@PathVariable String type) { return spendService.getPayTypeNum(type);}

    @ApiOperation("获取每年总的数据")
    @AnonymousAccess
    @GetMapping(value = "/getYearPriceSum/{year}}")
    public Integer getYearPriceSum(@PathVariable String year) {
        return spendService.getYearPriceSum(year);
    }


    @ApiOperation("获取总月份")
    @AnonymousAccess
    @GetMapping(value = "/monthDateList/{numMonth}")
    public List<Map<Object,Object>> monthDateList(@PathVariable Integer numMonth) {
        return spendService.monthDateList(numMonth);
    }

    @ApiOperation("消费类型字典值")
    @AnonymousAccess
    @GetMapping(value = "/getSpendLabel")
    public List<String> getSpendLabel() {
        return spendService.getSpendLabel();
    }


    @GetMapping("/countNumberByDate/{dates}")
    @Log("查询近7天的数据")
    @ApiOperation("查询近7天的数据")
    public Map<String, Object> countNumberByDate(@PathVariable String dates) {
        return spendService.countNumberByDate(dates);
    }

    @GetMapping("/file")
    @AnonymousAccess
    public void fileTo() {

        spendService.orginToDesnate();

    }



}